package com.magicode.portal.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.magicode.cms.entiy.base.BuyHistory;
import com.magicode.cms.entiy.base.ChannelBuyHistory;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Invoice;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsBuyHistory;
import com.magicode.cms.entiy.base.OfflineEventsUserBuyHistory;
import com.magicode.cms.entiy.base.PayHistory;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.UserCoin;
import com.magicode.cms.entiy.base.UserCoinHistory;
import com.magicode.cms.entiy.base.VipBuyHistory;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.dao.PageResults;
import com.magicode.portal.dao.IMoneyDao;
@Repository("MoneyDao")
public class MoneyDaoImpl implements IMoneyDao{
	public void refreshTransaction(BuyHistory history,UserCoin ucion,UserCoinHistory ucionhistory)throws Exception{
		if(null!=history){
			history.setEndTime(new Date());
			commonDaoService.update(history);
		}
		if(null!=ucionhistory){
			commonDaoService.save(ucionhistory);
		}
		if(null!=ucion){
			commonDaoService.update(ucion);
		}
	}
	//充值成功后 修改用户豆币余额及添加豆币历史
	public void returnPayHistory(PayHistory history,UserCoin ucion,UserCoinHistory ucionhistory)throws Exception{
		if(null!=history){
			history.setEndTime(new Date());
			commonDaoService.update(history);
		}
		if(null!=ucionhistory){
			commonDaoService.save(ucionhistory);
		}
		if(null!=ucion){
			commonDaoService.update(ucion);
		}
		
	}
	
	@Transactional
	//添加充值记录
	public boolean addPayHistory(PayHistory history)throws Exception{
		// 购买豆币历史
		if(null!=history){
			history.setBeginTime(new Date());
			commonDaoService.save(history);
		}
		return true ;
	}
	/**
	 * 添加 购买记录
	 */
	@Override
	public boolean addBuyHistory(BuyHistory history) throws Exception {
		try {
			// TODO Auto-generated method stub
			history.setBeginTime(new Date());
			commonDaoService.save(history);
		} catch (Exception e) {
			// TODO: handle exception
			throw new Exception("create BuyHistory obj error ");
		}
		return true;
	}

	
	
	@Override
	public List<PayHistory> getUserCoinHistorys(Map<String, String> prams) throws Exception{
		String hql = " select t from "+PayHistory.class.getName()+" t  left outer join fetch t.payType left outer join fetch t.status left outer join fetch t.invoicestatus ";
		String userid = prams.get("userid");
		String wherestr=" where  t.user.id =:id ";
		String orderstr= " order by t.beginTime desc";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",userid);
		return (List<PayHistory>)commonDaoService.queryList(hql+wherestr+orderstr, map);
		
	}
	@Override
	public List<Object> queryMoneyList(String hql, int start, int size)
			throws Exception {
		return  commonDaoService.queryList(hql, start, size, null);
	}

	@Override
	public List<BuyHistory> getUserBuyHistorys(Map<String,String> prams)throws Exception {
		String hql = " select t from ";
		String tableName = BuyHistory.class.getName();
		String afterTableName = " t  left outer join fetch t.status  ";
		String queryType = prams.get("queryType");
		if(null != queryType){
			if("normal".equals(queryType)){
				tableName = VipBuyHistory.class.getName();
			}
			if("channel".equals(queryType)){
				tableName = ChannelBuyHistory.class.getName();
			}
			if("offline".equals(queryType)){
				tableName = OfflineEventsUserBuyHistory.class.getName();
			}
		}
		String wherestr=" where 1=1 and t.user.id=:id ";
		String orderstr= " order by t.beginTime desc";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",prams.get("userid"));
		return commonDaoService.queryList(hql+tableName + afterTableName + wherestr+orderstr, map);
	}
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;

	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}

	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}


	@Override
	public NewsBuyHistory getNewsByBuyHistoryId(String newsBuyHistoryid)throws Exception {
	
		String hql =" from "+NewsBuyHistory.class.getName()+" t left outer join fetch t.news where t.id=:id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",newsBuyHistoryid);
		List list = commonDaoService.queryList(hql, map);
		if(null!=list&&list.size()>0)
			return (NewsBuyHistory)list.get(0);
		return null;
	}
	
	@Override
	public OfflineEventsUserBuyHistory getOfflineBuyHis(String id) throws Exception {
		String hql =" from "+OfflineEventsUserBuyHistory.class.getName()+" t left outer join fetch t.offlineEventsUser where t.id=:id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",id);
		List list = commonDaoService.queryList(hql, map);
		if(null!=list&&list.size()>0)
			return (OfflineEventsUserBuyHistory)list.get(0);
		return null;
	}
	@Override
	public ChannelBuyHistory getChannelBuyHistoryById(String id)
			throws Exception {
		String hql =" from "+ChannelBuyHistory.class.getName()+" t left outer join fetch t.channel where t.id=:id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",id);
		List list = commonDaoService.queryList(hql, map);
		if(null!=list&&list.size()>0)
			return (ChannelBuyHistory)list.get(0);
		return null;
	}
	@Override
	public PayHistory getPayHistoryByid(String id) throws Exception {
		//return (PayHistory) commonDaoService.get(PayHistory.class, id);
		String hql =" from "+PayHistory.class.getName()+" t left outer join fetch t.status left outer join fetch t.qrCodePic left outer join fetch t.invoicestatus left outer join fetch t.user where t.id=:id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",id);
		List list = commonDaoService.queryList(hql, map);
		if(null!=list&&list.size()>0)
			return (PayHistory)list.get(0);
		return null;
	}
	public BuyHistory getBuyHistoryByid(String id)throws Exception{
		String hql =" from "+BuyHistory.class.getName()+" t left outer join fetch t.user left outer join fetch t.status where t.id=:id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id",id);
		List list = commonDaoService.queryList(hql, map);
		if(null!=list&&list.size()>0)
			return (BuyHistory)list.get(0);
		return null;
	}
	public void setBuyHistoryPayHistory(String buyid,String payid)throws Exception{
		BuyHistory buy = this.getBuyHistoryByid(buyid);
		PayHistory pay =  this.getPayHistoryByid(payid);
		buy.setPayHistory(pay);
		commonDaoService.update(buy);
	}
	public ICommonDaoService getICommonDaoService(){
		return commonDaoService;
	}
	@Override
	public void setBuyHistoryStatus(String buyid,Dictionary dic) throws Exception {
		BuyHistory history = this.getBuyHistoryByid(buyid);
		history.setStatus(dic);
		commonDaoService.update(history);
		
		
	}
	@Override
	public void editObject(PayHistory history) throws Exception {
		commonDaoService.update(history);
	}
}
